package tsj.ai.alibaba.data.analysis.common.enums;

/**
 * @author taoshujian
 * @version 1.0
 * @since 2025-11-14 11:44:47
 */
public enum BizDataSourceTypeEnum {
    MYSQL(1, "mysql", DatabaseDialectEnum.MYSQL.getCode(), DbAccessTypeEnum.JDBC.getCode()),

    POSTGRESQL(2, "postgresql", DatabaseDialectEnum.POSTGRESQL.getCode(), DbAccessTypeEnum.JDBC.getCode()),

    SQLITE(3, "sqlite", DatabaseDialectEnum.MYSQL.getCode(), DbAccessTypeEnum.JDBC.getCode()),

    H2(4, "h2", DatabaseDialectEnum.H2.getCode(), DbAccessTypeEnum.JDBC.getCode()),

    HOLOGRESS(10, "hologress", DatabaseDialectEnum.POSTGRESQL.getCode(), DbAccessTypeEnum.JDBC.getCode()),

    MYSQL_VPC(11, "mysql-vpc", DatabaseDialectEnum.MYSQL.getCode(), DbAccessTypeEnum.JDBC.getCode()),

    POSTGRESQL_VPC(12, "postgresql-vpc", DatabaseDialectEnum.POSTGRESQL.getCode(), DbAccessTypeEnum.JDBC.getCode()),

    ADB_PG(21, "adg_pg", DatabaseDialectEnum.POSTGRESQL.getCode(), DbAccessTypeEnum.DATA_API.getCode()),

    MAX_COMPUTE(31, "max_compute", DatabaseDialectEnum.MYSQL.getCode(), DbAccessTypeEnum.JDBC.getCode()),
    // SQLite simulated database in function computation
    FC_MEMORY_DB(41, "fc_memory_db", DatabaseDialectEnum.SQLite.getCode(), DbAccessTypeEnum.FC_HTTP.getCode()),

    MYSQL_VIRTUAL(51, "mysql-virtual", DatabaseDialectEnum.MYSQL.getCode(), DbAccessTypeEnum.MEMORY.getCode()),

    POSTGRESQL_VIRTUAL(52, "postgresql-virtual", DatabaseDialectEnum.POSTGRESQL.getCode(),
            DbAccessTypeEnum.MEMORY.getCode());

    public final Integer code;

    public final String typeName;

    public final String dialect;

    public final String protocol;

    BizDataSourceTypeEnum(Integer code, String typeName, String dialect, String protocol) {
        this.code = code;
        this.typeName = typeName;
        this.dialect = dialect;
        this.protocol = protocol;
    }

    public Integer getCode() {
        return code;
    }

    public String getTypeName() {
        return typeName;
    }

    public String getProtocol() {
        return protocol;
    }

    public String getDialect() {
        return dialect;
    }

    /**
     * Get corresponding typeName based on code.
     * @param code code for which to get typeName
     * @return corresponding typeName, return null if not found.
     */
    public static String getTypeNameByCode(Integer code) {
        for (BizDataSourceTypeEnum type : values()) {
            if (type.getCode().equals(code)) {
                return type.getTypeName();
            }
        }
        return null; // If corresponding code is not found, return null
    }

    public static String getDialectByCode(Integer code) {
        for (BizDataSourceTypeEnum type : values()) {
            if (type.getCode().equals(code)) {
                return type.getDialect();
            }
        }
        return null; // If corresponding code is not found, return null
    }

    public static String getProtocolByCode(Integer code) {
        for (BizDataSourceTypeEnum type : values()) {
            if (type.getCode().equals(code)) {
                return type.getProtocol();
            }
        }
        return null;
    }

    public static BizDataSourceTypeEnum fromCode(Integer code) {
        for (BizDataSourceTypeEnum type : values()) {
            if (type.getCode() == code) {
                return type;
            }
        }
        return null;
    }

    public static BizDataSourceTypeEnum fromTypeName(String typeName) {
        for (BizDataSourceTypeEnum type : values()) {
            if (type.getTypeName().equals(typeName)) {
                return type;
            }
        }
        return null;
    }

    public static boolean isMysqlDialect(String typeName) {
        return isDialect(typeName, DatabaseDialectEnum.MYSQL.getCode());
    }

    public static boolean isPgDialect(String typeName) {
        return isDialect(typeName, DatabaseDialectEnum.POSTGRESQL.getCode());
    }

    public static boolean isAdbPg(String typeName) {
        BizDataSourceTypeEnum te = fromTypeName(typeName);
        if (te == null) {
            return false;
        }
        if (DatabaseDialectEnum.POSTGRESQL.getCode().equalsIgnoreCase(te.getDialect())
                && DbAccessTypeEnum.DATA_API.getCode().equalsIgnoreCase(te.getProtocol())) {
            return true;
        }
        return false;
    }

    public static boolean isDialect(String typeName, String dialect) {
        BizDataSourceTypeEnum te = fromTypeName(typeName);
        if (te == null) {
            return false;
        }
        if (dialect.equalsIgnoreCase(te.getDialect())) {
            return true;
        }
        return false;
    }
}
